package org.me.Slide.Adapter;

import java.util.ArrayList;
import java.util.List;


import org.me.Slide.R;
import org.me.Slide.Util.FileUtil;
import org.me.Slide.Util.XMLUtil;
import org.me.Slide.data.Albums;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class AlbumGridAdapter extends BaseAdapter {
	
	private Context mContext = null;
	private List<Albums> listAlbum = null;
	private LayoutInflater mInflater = null;
	private Bitmap bitmap;
	private String filename, folder;
	private BitmapDrawable bitmapDrawable;
	
	public static byte[] byteBuffer = new byte[128];
	
	
	public AlbumGridAdapter(Context context, List<Albums> album, String _folder)
	{
		mContext = context;
		
		listAlbum = album;
		folder =_folder;
		
		mInflater = LayoutInflater.from(context);
		
		
	}
	
	public class ViewHolder
	{
		ImageView imageView;
		LinearLayout linear;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return listAlbum.size() ;
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(final int position, View view, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		if(view == null)
		{
			holder = new ViewHolder();
			view = mInflater.inflate(R.layout.album_grid_activity_item, null);
			holder.imageView = (ImageView)view.findViewById(R.id.albums_grid_item_image_view);
			holder.linear = (LinearLayout)view.findViewById(R.id.albums_grid_item_layout_press);
			view.setTag(holder);
		}
		else
		{
			holder = (ViewHolder)view.getTag();
		}
		
		holder.imageView.setTag(position);
		holder.linear.setTag(position);
		
		
		filename = "images/"+folder+"/smallimage/"+listAlbum.get(position).getImage();
		
		byteBuffer  = FileUtil.readFileFromAssets(mContext, filename);
		
		bitmap = BitmapFactory.decodeByteArray(byteBuffer, 0, byteBuffer.length);
		
		bitmapDrawable = new BitmapDrawable(bitmap);
		holder.imageView.setImageDrawable(bitmapDrawable);
		
		byteBuffer = null;
		return view;
	}

	
}

